// pages/location/location.js
import { loadCities } from '../../api/url.js'
var QQMapWX = require('../../libs/qqmap-wx-jssdk.min.js');
const app = getApp()
var qqmapsdk
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentPosition: '',
    cityPosition: '',
    openCityPosition: '',
    cityData: null,//城市数据
    currentCity: null,//目前选择的城市
    currentLocation: null,//目前定位
    showAuthorize: false,//是否显示地理位置授权框
  },
  /**
   * @method
   * @desc 加载城市地质
   */
  loadLocation(id) {
    let that = this
    let query
    if (id) {
      query = {
        loginName: app.globalData.userInfo.loginname,
        token: wx.getStorageSync('token'),
        page: 1,
        size: 9999,
        cityId: id
      }
    } else {
      query = {
        loginName: app.globalData.userInfo.loginname,
        token: wx.getStorageSync('token'),
        page: 1,
        size: 9999
      }
    }
    wx.request({
      url: loadCities,
      data: query,
      success(res) {
        console.log(res)
        if (res.data.code === 0) {
          that.setData({
            cityData: res.data.response.cont
          })
        } else {
          wx.showToast({
            title: '加载城市异常',
            icon: 'none'
          })
        }

      },
      fail() {
        wx.showToast({
          title: '加载城市失败',
          icon: 'none'
        })
      }
    })
  },
  /**
   * @method
   * @desc 选择城市
   */
  setCurrentCity(e) {
    let that = this
    let item = e.currentTarget.dataset.item
    that.loadLocation(item.fId)
    that.setData({
      currentCity: item
    })
  },
  /**
   * @method
   * @desc 获取地理定位
   */
  getLocation() {
    let that = this
    //1、获取当前位置坐标
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        //2、根据坐标获取当前位置名称，显示在顶部:腾讯地图逆地址解析
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function (addressRes) {
            if (addressRes.status === 0) {
              that.setData({
                currentLocation: addressRes.result
              })
            }
          }
        })
      }
    })
  },
  /**
   * @callback
   * @desc 授权回调
   */
  settingCB(res) {//授权回调
    let that = this
    console.log(res)
    if (res.detail.authSetting["scope.userLocation"]){
      that.setData({
        showAuthorize: false
      })
      that.getLocation()
    }
  },
  /**
   * @method
   * @desc 关闭授权框
   */
  closeModel(){
    let that=this
    that.setData({
      showAuthorize:false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this
    wx.createSelectorQuery().select('#currentLocation').boundingClientRect(function (res) {
      self.setData({
        currentPosition: res.width / 2
      })
    }).exec()
    wx.createSelectorQuery().select('#cityLocation').boundingClientRect(function (res) {
      self.setData({
        cityPosition: res.width / 2
      })
    }).exec()
    wx.createSelectorQuery().select('#openCity').boundingClientRect(function (res) {
      self.setData({
        openCityPosition: res.width / 2
      })
    }).exec()
    self.loadLocation()
    //腾讯地图sdk
    qqmapsdk = new QQMapWX({
      key: 'UDJBZ-HLDC3-VKG3T-YHIJG-6ICYE-OOB2J'
    })
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {//非初始化进入该页面,且未授权
          self.setData({
            showAuthorize: true
          })
        } else if (res.authSetting['scope.userLocation'] == undefined || res.authSetting['scope.userLocation']) {//初始化进入
          self.getLocation()
        }
      },
      fail() {
        wx.showToast({
          title: '获取授权信息失败',
          icon: 'none'
        })
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})